package com.wxzz.elearing.user.api.app;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.user.dto.*;
import com.wxzz.elearing.user.entity.CommonCategory;
import com.wxzz.elearing.user.entity.Docent;
import com.wxzz.elearing.user.entity.DocentTool;
import com.wxzz.elearing.user.entity.DocentTrial;
import com.wxzz.elearing.user.service.ICommonCategoryService;
import com.wxzz.elearing.user.service.IDocentService;
import com.wxzz.elearing.user.service.ISystemMessageService;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/wx-docent")
@Api(tags = "微信讲师接口")
public class WxDocentController {

    @Autowired
    private IDocentService docentService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @ApiOperation(value = " 讲师列表(小程序)", notes = " 讲师列表(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "wxListDocent", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<Docent>>> wxListDocent(@RequestHeader String token, @RequestBody DocentDto docentDto) {
        //小程序只显示审核通过的
        docentDto.setStatus(1);
        //docentDto.setAppraisalStatus(2);
        if (docentDto.getType() == 0) {
            return docentService.listDocent(0L, docentDto);
        }
        if (docentDto.getType() == 1) {
            JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
            return docentService.listDocent(jsonObject.getLong("enterpriseId"), docentDto);
        }
        throw new ServiceException("请输入type 0-平台 1-企业");
    }

    @ApiOperation(value = " 创建试讲账号(小程序)", notes = " 创建试讲账号(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "sendDocent", method = RequestMethod.POST)
    public ReturnVO sendDocent(@RequestHeader String token, @RequestBody DocentInsDto docentInsDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        docentInsDto.setWxUserId(jsonObject.getLong("id"));
        return docentService.sendDocent(docentInsDto);
    }

    @ApiOperation(value = " 提交讲师申请(小程序)", notes = " 提交讲师申请(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "sendDocentApply", method = RequestMethod.POST)
    public ReturnVO sendDocentApply(@RequestHeader String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return docentService.sendDocentApply(jsonObject);
    }

    @ApiOperation(value = "根据用户id查询讲师审核状态  0-待审核 1-已通过 2-未通过(小程序)", notes = "根据用户id查询讲师审核状态  0-待审核 1-已通过 2-未通过(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectStatus", method = RequestMethod.POST)
    public ReturnVO selectStatus(@RequestHeader String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return docentService.selectStatus(jsonObject.getLong("id"));
    }

    @Autowired
    private ICommonCategoryService commonCategoryService;

    @ApiOperation(value = " 列出讲师分类(后台)", notes = " 列出讲师分类(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listCommonCategoryByType", method = RequestMethod.POST)
    public ReturnVO<List<CommonCategory>> listCommonCategoryByType() {
        return commonCategoryService.listCommonCategoryByType(0);
    }

    @ApiOperation(value = " 试讲列表(小程序)", notes = " 试讲列表(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listDocentTrial", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<DocentTrial>>> listDocentTrial(@RequestHeader String token, @RequestBody WxDocentTrialDto wxDocentTrialDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return docentService.wxListDocentTrial(0, jsonObject.getLong("id"), wxDocentTrialDto);
    }

    @ApiOperation(value = "晋升列表(小程序)", notes = " 晋升列表(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listPromotion", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<DocentTrial>>> listPromotion(@RequestHeader String token, @RequestBody WxDocentTrialDto wxDocentTrialDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return docentService.wxListDocentTrial(1, jsonObject.getLong("id"), wxDocentTrialDto);
    }

    @ApiOperation(value = "试讲发布至晋升(小程序)", notes = "试讲发布至晋升", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "sendDocentTrialToPromotion/{docentTrialId}", method = RequestMethod.POST)
    public ReturnVO sendDocentTrialToPromotion(@PathVariable Long docentTrialId) {
        return docentService.sendDocentTrialToPromotion(docentTrialId);
    }

    @ApiOperation(value = " 讲师工具列表(小程序)", notes = " 讲师工具列表(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listDocentTool", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<DocentTool>>> listDocentTool(@RequestHeader String token, @RequestBody DocentToolDto docentToolDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return docentService.listDocentTool(jsonObject.getLong("enterpriseId"), docentToolDto);
    }

    @ApiOperation(value = " 查询讲师详情(小程序)", notes = " 查询讲师详情(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectDocnetById/{docentId}", method = RequestMethod.POST)
    public ReturnVO selectDocentById4mini(@PathVariable Long docentId) {
        return docentService.selectDocentById4mini(docentId);
    }

    @ApiOperation(value = " 试讲/晋升详情(小程序)", notes = " 试讲/晋升详情(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectDocentTrialDetail/{docentTrialId}", method = RequestMethod.POST)
    public ReturnVO selectDocentTrialDetail(@PathVariable Long docentTrialId) {
        return docentService.selectDocentTrialDetail(docentTrialId);
    }

    @ApiOperation(value = " 讲师工具详情(小程序)", notes = " 讲师工具详情(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectDocentToolDetail/{docentToolId}", method = RequestMethod.POST)
    public ReturnVO selectDocentToolDetail(@PathVariable Long docentToolId) {
        return docentService.selectDocentToolDetail(docentToolId);
    }


}
